home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / vidhrdw / suprloco.c < prev    next >
C/C++ Source or Header  |  2000-05-04  |  7KB  |  295 lines

  1. /***************************************************************************
  2.  
  3.   vidhrdw.c
  4.  
  5.   Functions to emulate the video hardware of the machine.
  6.  
  7. ***************************************************************************/
  8.  
  9. #include "driver.h"
  10.  
  11.  
  12. extern unsigned char *spriteram;
  13. extern size_t spriteram_size;
  14.  
  15. unsigned char *suprloco_videoram;
  16.  
  17. static struct tilemap *bg_tilemap;
  18. static int flipscreen;
  19. static int control;
  20.  
  21. #define SPR_Y_TOP        0
  22. #define SPR_Y_BOTTOM    1
  23. #define SPR_X            2
  24. #define SPR_COL            3
  25. #define SPR_SKIP_LO        4
  26. #define SPR_SKIP_HI        5
  27. #define SPR_GFXOFS_LO    6
  28. #define SPR_GFXOFS_HI    7
  29.  
  30.  
  31. /***************************************************************************
  32.  
  33.   Convert the color PROMs into a more useable format.
  34.  
  35.   I'm not sure about the resistor values, I'm using the Galaxian ones.
  36.  
  37. ***************************************************************************/
  38. void suprloco_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom)
  39. {
  40.     int i;
  41.  
  42.  
  43.     for (i = 0;i < Machine->drv->total_colors; i++)
  44.     {
  45.         int bit0,bit1,bit2;
  46.  
  47.         /* red component */
  48.         bit0 = (*color_prom >> 0) & 0x01;
  49.         bit1 = (*color_prom >> 1) & 0x01;
  50.         bit2 = (*color_prom >> 2) & 0x01;
  51.         *(palette++) = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
  52.         /* green component */
  53.         bit0 = (*color_prom >> 3) & 0x01;
  54.         bit1 = (*color_prom >> 4) & 0x01;
  55.         bit2 = (*color_prom >> 5) & 0x01;
  56.         *(palette++) = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
  57.         /* blue component */
  58.         bit0 = 0;
  59.         bit1 = (*color_prom >> 6) & 0x01;
  60.         bit2 = (*color_prom >> 7) & 0x01;
  61.         *(palette++) = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
  62.  
  63.         color_prom++;
  64.     }
  65. }
  66.  
  67.  
  68.  
  69. /***************************************************************************
  70.  
  71.   Callbacks for the TileMap code
  72.  
  73. ***************************************************************************/
  74.  
  75. static void get_tile_info(int tile_index)
  76. {
  77.     unsigned char attr = suprloco_videoram[2*tile_index+1];
  78.     SET_TILE_INFO(0,suprloco_videoram[2*tile_index] | ((attr & 0x03) << 8),(attr & 0x1c) >> 2)
  79.     tile_info.priority = (attr & 0x20) >> 5;
  80. }
  81.  
  82.  
  83.  
  84. /***************************************************************************
  85.  
  86.   Start the video hardware emulation.
  87.  
  88. ***************************************************************************/
  89.  
  90. int suprloco_vh_start(void)
  91. {
  92.     bg_tilemap = tilemap_create(get_tile_info,tilemap_scan_rows,TILEMAP_OPAQUE,8,8,32,32);
  93.  
  94.     if (!bg_tilemap)
  95.         return 1;
  96.  
  97.     tilemap_set_scroll_rows(bg_tilemap,32);
  98.  
  99.     return 0;
  100. }
  101.  
  102.  
  103.  
  104. /***************************************************************************
  105.  
  106.   Memory handlers
  107.  
  108. ***************************************************************************/
  109.  
  110. WRITE_HANDLER( suprloco_videoram_w )
  111. {
  112.     if (suprloco_videoram[offset] != data)
  113.     {
  114.         suprloco_videoram[offset] = data;
  115.         tilemap_mark_tile_dirty(bg_tilemap,offset/2);
  116.     }
  117. }
  118.  
  119. static int suprloco_scrollram[32];
  120.  
  121. WRITE_HANDLER( suprloco_scrollram_w )
  122. {
  123.     int adj = flipscreen ? -8 : 8;
  124.  
  125.     suprloco_scrollram[offset] = data;
  126.     tilemap_set_scrollx(bg_tilemap,offset, data - adj);
  127. }
  128.  
  129. READ_HANDLER( suprloco_scrollram_r )
  130. {
  131.     return suprloco_scrollram[offset];
  132. }
  133.  
  134. WRITE_HANDLER( suprloco_control_w )
  135. {
  136.     /* There is probably a palette select in here */
  137.  
  138.        /* Bit 0   - coin counter A */
  139.     /* Bit 1   - coin counter B (only used if coinage differs from A) */
  140.     /* Bit 2-3 - probably unused */
  141.     /* Bit 4   - ??? pulsated when loco turns "super" */
  142.     /* Bit 5   - ??? */
  143.     /* Bit 6   - probably unused */
  144.     /* Bit 7   - flip screen */
  145.  
  146.     if ((control & 0x10) != (data & 0x10))
  147.     {
  148.         /*logerror("Bit 4 = %d\n", (data >> 4) & 1); */
  149.     }
  150.  
  151.     if ((control & 0x20) != (data & 0x20))
  152.     {
  153.         /*logerror("Bit 5 = %d\n", (data >> 5) & 1); */
  154.     }
  155.  
  156.     coin_counter_w(0, data & 0x01);
  157.     coin_counter_w(1, data & 0x02);
  158.  
  159.     flipscreen = data & 0x80;
  160.     tilemap_set_flip(ALL_TILEMAPS,flipscreen ? (TILEMAP_FLIPY | TILEMAP_FLIPX) : 0);
  161.     tilemap_set_scrolly(bg_tilemap,0,flipscreen ? -32 : 0);
  162.  
  163.     control = data;
  164. }
  165.  
  166.  
  167. READ_HANDLER( suprloco_control_r )
  168. {
  169.     return control;
  170. }
  171.  
  172.  
  173.  
  174. /***************************************************************************
  175.  
  176.   Draw the game screen in the given osd_bitmap.
  177.   Do NOT call osd_update_display() from this function, it will be called by
  178.   the main emulation engine.
  179.  
  180. ***************************************************************************/
  181.  
  182. INLINE void draw_pixel(struct osd_bitmap *bitmap,int x,int y,int color)
  183. {
  184.     if (flipscreen)
  185.     {
  186.         x = bitmap->width - x - 1;
  187.         y = bitmap->height - y - 1;
  188.     }
  189.  
  190.     if (x < Machine->drv->visible_area.min_x ||
  191.         x > Machine->drv->visible_area.max_x ||
  192.         y < Machine->drv->visible_area.min_y ||
  193.         y > Machine->drv->visible_area.max_y)
  194.         return;
  195.  
  196.     plot_pixel(bitmap, x, y, color);
  197. }
  198.  
  199.  
  200. static void render_sprite(struct osd_bitmap *bitmap,int spr_number)
  201. {
  202.     int sx,sy,col,row,height,src,adjy,dy;
  203.     unsigned char *spr_reg;
  204.     unsigned short *spr_palette;
  205.     short skip;    /* bytes to skip before drawing each row (can be negative) */
  206.  
  207.  
  208.     spr_reg    = spriteram + 0x10 * spr_number;
  209.  
  210.     src = spr_reg[SPR_GFXOFS_LO] + (spr_reg[SPR_GFXOFS_HI] << 8);
  211.     skip = spr_reg[SPR_SKIP_LO] + (spr_reg[SPR_SKIP_HI] << 8);
  212.  
  213.     height        = spr_reg[SPR_Y_BOTTOM] - spr_reg[SPR_Y_TOP];
  214.     spr_palette    = Machine->remapped_colortable + 0x10 * spr_reg[SPR_COL];
  215.     sx = spr_reg[SPR_X];
  216.     sy = spr_reg[SPR_Y_TOP] + 1;
  217.  
  218.     if (!flipscreen)
  219.     {
  220.         adjy = sy;
  221.         dy = 1;
  222.     }
  223.     else
  224.     {
  225.         adjy = sy + height + 30;  /* some of the sprites are still off by a pixel */
  226.         dy = -1;
  227.     }
  228.  
  229.     for (row = 0;row < height;row++,adjy+=dy)
  230.     {
  231.         int color1,color2,flipx;
  232.         UINT8 data;
  233.         UINT8 *gfx;
  234.  
  235.         src += skip;
  236.  
  237.         col = 0;
  238.  
  239.         /* get pointer to packed sprite data */
  240.         gfx = &(memory_region(REGION_GFX2)[src & 0x7fff]);
  241.         flipx = src & 0x8000;   /* flip x */
  242.  
  243.         while (1)
  244.         {
  245.             if (flipx)    /* flip x */
  246.             {
  247.                 data = *gfx--;
  248.                 color1 = data & 0x0f;
  249.                 color2 = data >> 4;
  250.             }
  251.             else
  252.             {
  253.                 data = *gfx++;
  254.                 color1 = data >> 4;
  255.                 color2 = data & 0x0f;
  256.             }
  257.  
  258.             if (color1 == 15) break;
  259.             if (color1)
  260.                 draw_pixel(bitmap,sx+col,  adjy,spr_palette[color1]);
  261.  
  262.             if (color2 == 15) break;
  263.             if (color2)
  264.                 draw_pixel(bitmap,sx+col+1,adjy,spr_palette[color2]);
  265.  
  266.             col += 2;
  267.         }
  268.     }
  269. }
  270.  
  271. void draw_sprites(struct osd_bitmap *bitmap)
  272. {
  273.     int spr_number;
  274.     unsigned char *spr_reg;
  275.  
  276.  
  277.     for (spr_number = 0;spr_number < (spriteram_size >> 4);spr_number++)
  278.     {
  279.         spr_reg = spriteram + 0x10 * spr_number;
  280.         if (spr_reg[SPR_X] != 0xff)
  281.             render_sprite(bitmap,spr_number);
  282.     }
  283. }
  284.  
  285. void suprloco_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
  286. {
  287.     tilemap_update(ALL_TILEMAPS);
  288.  
  289.     tilemap_render(ALL_TILEMAPS);
  290.  
  291.     tilemap_draw(bitmap,bg_tilemap,0);
  292.     draw_sprites(bitmap);
  293.     tilemap_draw(bitmap,bg_tilemap,1);
  294. }
  295.